package com.yutel.amote;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class SensorActivity extends Activity implements SensorEventListener{
	final String TAG = "Sensor";
	SensorManager smanager = null;
	Sensor sensor = null;
	TextView textInfo = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensorcfg);
        textInfo = (TextView)findViewById(R.id.sensor);
        smanager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
    @Override
	protected void onResume() {
		super.onResume();
		smanager.registerListener(
				this,
				smanager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER
						| SensorManager.SENSOR_ORIENTATION),
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onStop() {
		super.onStop();
		smanager.unregisterListener(this);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		Log.d(TAG,"传感器  Sensor:"+sensor.getName());
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		float[] values =event.values;
		synchronized (this) {
			StringBuffer sb = new StringBuffer();
			sb.append("坐标 X:").append(values[0]).append("\n");
			sb.append("坐标 Y:").append(values[1]).append("\n");
			sb.append("坐标 Z:").append(values[2]).append("\n");
			textInfo.setText(sb.toString());
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.control, menu);
		menu.removeItem(R.id.menui_server_control);
		menu.removeItem(R.id.menui_touch_config);
		menu.removeItem(R.id.menui_client_sensor);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menui_appType:
			Intent apptype = new Intent(SensorActivity.this,
					ApptypeActivity.class);
			startActivity(apptype);
			break;
		case R.id.menui_server_control:
			Log.d(TAG, "menui_server_control");
			break;
		case R.id.menui_client_touch:
			Intent touch = new Intent(SensorActivity.this,
					TouchActivity.class);
			startActivity(touch);
			break;
		case R.id.menui_touch_config:
			Log.d(TAG, "menui_touch_config");
			break;
		case R.id.menui_client_sensor:
			Log.d(TAG, "menui_client_sensor");
			break;
		case R.id.menui_client_borad:
			Intent borad = new Intent(SensorActivity.this, BoardActivity.class);
			startActivity(borad);
			break;
		default:
			Log.e(TAG, "default=" + item.getItemId());
		}
		return true;
	}
}